package com.letter.otools.day.util;

import static com.blankj.utilcode.util.ScreenUtils.getScreenHeight;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;

import com.blankj.utilcode.util.ConvertUtils;
import com.letter.otools.day.R;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.core.ImageViewerPopupView;
import com.lxj.xpopup.enums.PopupAnimation;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.lxj.xpopup.interfaces.OnSrcViewUpdateListener;
import com.lxj.xpopup.util.SmartGlideImageLoader;

import java.util.ArrayList;
import java.util.List;

public class PopupUtils {

    public static BasePopupView showBottomSheet(Context context, String title, String[] items, OnSelectListener selectListener) {
        return new XPopup.Builder(context)
                .maxHeight((int) (getScreenHeight() * 0.7))
                .moveUpToKeyboard(true)
                .autoDismiss(true)
                .asBottomList(title, items, selectListener).show();
    }

    public static void showBottomSheet(Context context, String title, List<String> items, OnSelectListener selectListener) {
        new XPopup.Builder(context)
                .autoDismiss(true)
                .asBottomList(title, items.toArray(new String[0]), selectListener).show();
    }

    public static void showLoading(Context context) {
        final BasePopupView loadingPopup = new XPopup.Builder(context).asLoading();
        loadingPopup.show();
    }

    public static void showConfirm(Context context, String title, String content) {
        new XPopup.Builder(context).asConfirm(title, content, new OnConfirmListener() {
            @Override
            public void onConfirm() {

            }
        }).show();
    }

    public static void preViewImages(Context context, ImageView imageView, int position, List<String> urls) {
        List<Object> urlList = new ArrayList<>();
        for (String s : urls) {
            urlList.add(s);
        }
        // 多图片场景
        new XPopup.Builder(context)
//                            .animationDuration(1000)
                .isTouchThrough(true)
                .asImageViewer(imageView, position, urlList,
                        false, true, -1, -1, ConvertUtils.dp2px(10), true,
                        Color.rgb(32, 36, 46),
                        new OnSrcViewUpdateListener() {
                            @Override
                            public void onSrcViewUpdate(ImageViewerPopupView popupView, int position) {
//                                RecyclerView rv = (RecyclerView) holder.itemView.getParent();
//                                popupView.updateSrcView((ImageView) rv.getChildAt(position));
                            }
                        }, new SmartGlideImageLoader(true, R.drawable.ic_default_image), null)
                .show();
    }

    public static void showAttachMenu(Context context, View atView, String[] menu, OnSelectListener listener) {
        new XPopup.Builder(context)
                .atView(atView)
                .dismissOnTouchOutside(true)
                .isViewMode(true)      //开启View实现
                .isRequestFocus(false) //不强制焦点
                .isTouchThrough(true)
                .hasShadowBg(false)
                .positionByWindowCenter(true)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                .asAttachList(menu, null, listener).show();
    }
}
